package ocean.fauna.I;

import java.awt.Point;
import java.util.ArrayList;

import ocean.data.DataHolder;
import ocean.data.I.IdBuilder;
import ocean.service.FishStepGenerator;

public abstract class Fish   implements Cloneable,IdBuilder
{
    protected int id;

    protected Point position;

    protected String type;

    protected int eatDaysAgo;
    protected int daysWithoutFood;

    protected int lifeTime;
    protected int wasBorn;

    protected int clonePeriod;
    protected int cloneCount;

    protected ArrayList<Integer> idCanEat;

    public Fish(Fish f)
    {
	id = f.getId();
	idCanEat = f.getIdCanEat();
	clonePeriod = f.getClonePeriod();
	lifeTime = f.getLifeTime();
    }

    public Fish(int idd, ArrayList<Integer> idCanEatt, int clonePeriodd, int lifeTimee)
    {
	id = idd;
	idCanEat = idCanEatt;
	clonePeriod = clonePeriodd;
	lifeTime = lifeTimee;
    }

    public void eat()
    {
	for (int id : idCanEat)
	{
	    if (DataHolder.getPositionsObjMatrix()[position.x][position.y] == id)
	    {
		DataHolder.getPositionsObjMatrix()[position.x][position.y] = 0;
		eatDaysAgo = 0;
	    }
	}
    }

   

    public boolean isLive()
    {
	return (lifeTime == wasBorn || eatDaysAgo == daysWithoutFood) ? true : false;
    }

    @Override
    public abstract Object clone();
    
    public boolean move() throws CloneNotSupportedException
    {
	boolean res = isLive();

	if (res)
	{
	    eatDaysAgo++;
	    lifeTime++;
	    cloneCount++;

	    FishStepGenerator.getFreePosition(this);
	    eat();

	    if (cloneCount == clonePeriod)
	    {
		DataHolder.getOceanFauna().add((Fish) this.clone());
		cloneCount = 0;
	    }

	}

	return res;
    }

    public int getId()
    {
	return id;
    }

    public void setId(int id)
    {
	this.id = id;
    }

    public Point getPosition()
    {
	return position;
    }

    public void setPosition(Point position)
    {
	this.position = position;
    }

    public String getType()
    {
	return type;
    }

    public void setType(String type)
    {
	this.type = type;
    }

    public int getEatDaysAgo()
    {
	return eatDaysAgo;
    }

    public void setEatDaysAgo(int eatDaysAgo)
    {
	this.eatDaysAgo = eatDaysAgo;
    }

    public int getDaysWithoutFood()
    {
	return daysWithoutFood;
    }

    public void setDaysWithoutFood(int daysWithoutFood)
    {
	this.daysWithoutFood = daysWithoutFood;
    }

    public int getLifeTime()
    {
	return lifeTime;
    }

    public void setLifeTime(int lifeTime)
    {
	this.lifeTime = lifeTime;
    }

    public int getWasBorn()
    {
	return wasBorn;
    }

    public void setWasBorn(int wasBorn)
    {
	this.wasBorn = wasBorn;
    }

    public ArrayList<Integer> getIdCanEat()
    {
	return idCanEat;
    }

    public void setIdCanEat(ArrayList<Integer> idCanEat)
    {
	this.idCanEat = idCanEat;
    }

    public int getClonePeriod()
    {
	return clonePeriod;
    }

    public void setClonePeriod(int clonePeriod)
    {
	this.clonePeriod = clonePeriod;
    }

}
